package com.cdwh.joylife.presenter;

import android.content.Context;
import android.text.TextUtils;


import com.cdwh.joylife.net.ApiService;
import com.cdwh.joylife.net.MyFilter;
import com.cdwh.joylife.net.RetrofitHelper;
import com.cdwh.joylife.net.RxJavaUtils;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;


/**
 * Created by walln on 2016/10/10 0010.
 */

public abstract class BasePresenter {

    protected ApiService service;
    protected ViewInterface viewInterface;
    protected MyFilter filter;
    protected Observable observable;
    private Disposable disposable;

    public Observable getObservable() {
        return observable;
    }

    public BasePresenter(Context context, ViewInterface viewInterface) {
        service = RetrofitHelper.getRetrofit().create(ApiService.class);
        this.viewInterface = viewInterface;
        filter = new MyFilter(context, viewInterface);
    }

    public void setDispatch(boolean dispatch) {
        filter.setDispatch(dispatch);
    }

    public void setObservable(String msg, Observable observable) {
        this.setObservable(msg, observable, 0);
    }

    public void setObservable(final String msg, Observable observable, final int type) {
        this.observable = observable;
        if (!TextUtils.isEmpty(msg)) {
            viewInterface.showDialog(msg, true);
        }
        disposable = observable.compose(RxJavaUtils.applySchedulers())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (!TextUtils.isEmpty(msg)) {
                            viewInterface.disDialog();
                        }

                        filter.onSuccess(o, type);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        viewInterface.disDialog();

                        filter.onError(throwable, type);
                    }
                });
    }

    public void setObservable(final String msg, boolean cancelable, Observable observable, final int type) {
        this.observable = observable;
        if (!TextUtils.isEmpty(msg)) {
            viewInterface.showDialog(msg, cancelable);
        }
        disposable = observable.compose(RxJavaUtils.applySchedulers())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (!TextUtils.isEmpty(msg)) {
                            viewInterface.disDialog();
                        }

                        filter.onSuccess(o, type);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        viewInterface.disDialog();

                        filter.onError(throwable, type);
                    }
                });
    }

    public void cancle() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }
}
